//
//  GDBusLineViewController.m
//  GraduationDesign
//
//  Created by JiaKang.Zhong on 16/1/11.
//  Copyright © 2016年 JiaKang.Zhong. All rights reserved.
//

#import "GDBusLineViewController.h"
#import "MBProgressHUD+MJ.h"
#import "GDBusStationLineController.h"
#import "GDTansitionModelDateOperation.h"
#import "GDBuslLineViewCell.h"
@interface GDBusLineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) NSArray * statsModel;
@property(nonatomic,strong) NSArray * currentBusModel;
@property(nonatomic,weak) UITableView * tableView;
@property(nonatomic,weak) UILabel * updateTime;

@end

@implementation GDBusLineViewController


- (void)viewDidLoad {
    [super viewDidLoad];

 
 
    self.view.backgroundColor = [UIColor whiteColor];
    [MBProgressHUD showMessage:@"正在查询,请稍后..." toView:self.view];
    
    /**
     *  触发查询按钮后，获取到公交路线
     *
     */
    [GDTansitionModelDateOperation initDateWithBusStationLine:self success:^(NSArray *model) {
        if (model) {
            self.statsModel = model;
            [self updateUI:model];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:[NSString stringWithFormat:@"%@",error] toView:self.view];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    /**
     *  获取实时公交信息
     *
     */
//    [MBProgressHUD showMessage:@"正在获取实时公交信息.请稍等..." toView:self.view];
    [GDTansitionModelDateOperation initDateWithNowBusSationLocation:self success:^(NSArray *model) {
        self.currentBusModel = model;
        [self updateUI:nil];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:[NSString stringWithFormat:@"%@",error] toView:self.view];
    }];

}



#pragma mark 更新UI  转到别的控制器中更新UI
-(void) updateUI:(NSArray *) model{
    
    if (self.currentBusModel.count!= 0 && self.statsModel.count != 0) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        GDBusStationLinesModel * lineModel  = self.statsModel[0];
        GDLog(@"%@",lineModel.Name);
        
        // 图片
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(GDScreenSize.size.width * 0.1, 10, 54, 58)];
        imageView.image = [UIImage imageNamed:@"bus_icon"];
        [self.view addSubview:imageView];
        // 线路号
        UILabel * lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 10,imageView.frame.origin.y - 10, 300, 30)];
        lineLabel.textColor = [UIColor colorWithRed:196/255.0 green:0 blue:42/255.0 alpha:1];
        lineLabel.textAlignment = NSTextAlignmentLeft;
        lineLabel.text =  [NSString stringWithFormat:@"%@ [%@ -- %@]",self.busModel.LineNumber,self.busModel.FromStation,self.busModel.ToStation];
        [self.view addSubview:lineLabel];
        // 公交详细信息
        UILabel * infoLable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 10, CGRectGetMaxY(lineLabel.frame) - 10,200, 60)];
        infoLable.font = [UIFont systemFontOfSize:12];
        infoLable.textColor = [UIColor colorWithRed:196/255.0 green:0 blue:42/255.0 alpha:1];
        infoLable.text = [NSString stringWithFormat:@"首车:%@,末车:%@ 共有 %lu 辆行驶在路上",self.busModel.BeginTime,self.busModel.EndTime,(unsigned long)self.currentBusModel.count];
        
        infoLable.numberOfLines = 2;
        [self.view addSubview:infoLable];
        // 刷新数据
        CGFloat  fetchBtnX = GDScreenSize.size.width * 0.09;
        UIButton * fetchBtn = [[UIButton alloc] initWithFrame:CGRectMake(fetchBtnX, CGRectGetMaxY(imageView.frame), GDScreenSize.size.width - fetchBtnX * 2, 40)];
        [fetchBtn setTitle:@"刷新数据" forState:UIControlStateNormal];
        [fetchBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        fetchBtn.backgroundColor = [UIColor colorWithRed:53 / 255.0 green:171 / 255.0 blue:51 / 255.0 alpha:1];
        [self.view addSubview:fetchBtn];
        [fetchBtn addTarget:self action:@selector(fetchBtnClick) forControlEvents:UIControlEventTouchUpInside];
        // 刷新时间
        UILabel * updateTime = [[UILabel alloc] initWithFrame:CGRectMake(fetchBtnX, CGRectGetMaxY(fetchBtn.frame),300, 30)];
        updateTime.text = [self getCurrentTime];
        updateTime.font = [UIFont systemFontOfSize:GDLabelSmalFont];
        [self.view addSubview:updateTime];
        _updateTime = updateTime;
        
        // 显示数据
        UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(GDMargin, CGRectGetMaxY(updateTime.frame ), GDScreenSize.size.width - GDMargin * 2, GDScreenSize.size.height  - CGRectGetMaxY(updateTime.frame) - 66) style:UITableViewStylePlain ];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.layer.borderWidth = 0.5;
        tableView.layer.cornerRadius = 10;
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self.view addSubview:tableView];
        _tableView = tableView;
       
    }else{
        return;
    }
    
}


#pragma mark 刷新按钮
-(void) fetchBtnClick{
    /**
     *  获取实时公交信息
     *
     */
    __weak typeof(self) selfF =  self;
        [MBProgressHUD showMessage:@"正在获取实时公交信息.请稍等..." toView:self.view];
    [GDTansitionModelDateOperation initDateWithNowBusSationLocation:self success:^(NSArray *model) {
          [MBProgressHUD hideHUDForView:self.view animated:YES];
        selfF.currentBusModel = model;
        [selfF.tableView reloadData];
        selfF.updateTime.text = [selfF getCurrentTime];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:[NSString stringWithFormat:@"%@",error] toView:self.view];
    }];
}


#pragma mark 获取当前时间
-(NSString * ) getCurrentTime{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"最近更新时间 HH:mm";
    NSDate * now = [[NSDate alloc] init];
    NSString * string = [formatter stringFromDate:now];
    return string;
}

#pragma mark 行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.statsModel.count;
}

#pragma mark cell的内容
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * ID = @"cell";
    GDBuslLineViewCell* cell  = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell   = [[GDBuslLineViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }else{
        /*     移除所有的label元素     */
        for (UIView * view in [cell.nowBusView subviews]) {
            if ([view isKindOfClass:NSClassFromString(@"UILabel")]) {
                [view removeFromSuperview];
            }
        }
    }
    cell.nowBusArray = self.currentBusModel;
    cell.lineModel = self.statsModel[indexPath.row];
    
    return cell;

}

#pragma cell的高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return 44;
}
@end
